import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/record/points_logic.dart';
import 'package:sports_new/controller/record/team_logic.dart';
import 'package:sports_new/utils/utils.dart';
import '../../entity/record/channel_entity.dart';
import '../../view/record/player_page.dart';
import '../../view/record/points_page.dart';
import '../../view/record/schedule_page.dart';
import '../../view/record/team_page.dart';
import '../../view/record/transfer_page.dart';
import '../../widgets/bottom_picker_widget.dart';
import 'player_logic.dart';
import 'schedule_logic.dart';

class ChannelLogic extends GetxController {
  List<String> typeList = ["积分", "赛程", "球员", "球队", "转会"];
  List<String> umengList = ["赛季选择", "积分", "赛程", "球员", "球队", "转会"];
  PageController pageController = PageController();
  late ChannelEntity? data;
  int _recordType = 0;
  String _tag = "";
  String _season = "";
  int _seasonId = 0;
  int channelId = 0;
  int _seasonIndex = 0;
  int _typeIndex = 0;
  bool isLoading = true;
  List<Widget> page = [];

  int get seasonIndex => _seasonIndex;
  int get typeIndex => _typeIndex;
  String get season => _season;
  int get seasonId => _seasonId;
  int get recordType => _recordType;
  String get tag => _tag;

  set tag(String value) {
    _tag = value;
    update();
  }
  set recordType(int value) {
    _recordType = value;
    update();
  }
  set seasonId(int value) {
    _seasonId = value;
    update();
  }
  set season(String value) {
    _season = value;
    update();
  }
  set typeIndex(int value){
    _typeIndex = value;
    update();
  }

  Future getData(ChannelEntity? value) async {
    data = value;
    season = data?.seasonList?[_seasonIndex] ?? "";
    seasonId = data?.seasonIdList?[_seasonIndex].toInt() ?? 0;
    channelId = data?.channelId ?? 0;
    page = [
      PointsPage(leagueId: channelId, tag: tag),
      SchedulePage(leagueId: channelId, tag: tag),
      PlayerPage(channelId, tag: tag),
      TeamPage(channelId, tag: tag),
      if (data?.transfer == 1)
      ZqTransferPage(channelId, tag: tag)
    ];
    typeList.replaceAll([
      "积分",
      "赛程",
      "球员",
      "球队",
      if (data?.transfer == 1)
      "转会"]);
    isLoading = false;
    update();
  }

  Future showDatePicker() async {
    _seasonIndex = await Get.bottomSheet(BottomPickerWidget(
      data?.seasonList
        ?.map((e) => e.length > 4
          ? ("${e.substring(2, 4)}/${e.substring(7, 9)}") : e)
        .toList() ?? [],
      title: '选择赛季',
      initialIndex: _seasonIndex,
    ));
    _season = data?.seasonList?[_seasonIndex] ?? "";
    _seasonId = data?.seasonIdList?[_seasonIndex].toInt() ?? 0;
    getData(data);
    update();
  }

  doRefresh(){
    switch(typeList[typeIndex]){
      case "积分":
        Get.find<PointsLogic>(tag: tag).doRefresh();
        break;
      case "赛程":
        Get.find<ScheduleLogic>(tag: tag).doRefresh();
        break;
      case "球员":
        Get.find<PlayerLogic>(tag: tag).doRefresh();
        break;
      case "球队":
        Get.find<TeamLogic>(tag: tag).doRefresh();
        break;
    }
  }
}
